<?php

    require dirname(__FILE__) . 
        DIRECTORY_SEPARATOR . '..' . 
        DIRECTORY_SEPARATOR . '_include.php';
    
    class DataGateway {
        
        public function __construct() {}
        
        protected function insert($str_table, $arr_data) {
        
            $arr_values = array();
            foreach($arr_data as $mix) {
                $arr_values[] = is_string($mix) ? "'" . $mix . "'" : $mix;
            }
            
            $str_columns = implode(', ', array_keys($arr_data));
            $str_values = implode(', ', $arr_values);
            
            return 'INSERT INTO ' . $str_table . ' (' . $str_columns . ') VALUES (' . $str_values . ')' . "\n";
            
        }
    }
    
    class DataGateway_Master extends DataGateway {
        
        public function __construct() {
        }
        
        public function selectPressTitle($int_id) {
            
            echo __METHOD__ . "\n";
            
            echo "  - sql executed: SELECT * FROM press_title WHERE title_id = " . $int_id . "\n";
            
            $arr = array(
                145 => array(
                    'title_id' => 145,
                    'title_name' => 'Financial Times',
                    'title_slug' => 'financial-times',
                    'title_type' => 1
                ),
                256 => array(
                    'title_id' => 256,
                    'title_name' => 'GQ Magazine',
                    'title_slug' => 'gq-magazine',
                    'title_type' => 2
                )
            );
            
            if(isset($arr[$int_id])) {
                return array($arr[$int_id]);
            }
            return array();
        }
        
        public function insertPressTitle($arr_data) {
            
            echo __METHOD__ . "\n";
            
            if(!isset($arr_data['title_name']) || !isset($arr_data['title_slug']) || !isset($arr_data['title_type'])) {
                throw new Exception('Could not insert a new press title record because not required data is present.');
            }
            
            $arr_values = array(
                'title_name' => $arr_data['title_name'],
                'title_slug' => $arr_data['title_slug'],
                'title_type' => $arr_data['title_type']
            );
            
            echo "  - sql executed: " . $this->insert('press_title', $arr_values);
            
            $int_title_id = rand(0,10000);
            
            if(isset($arr_data['categories']) && is_array($arr_data['categories'])) {
                $this->insertPressTitleCategories($int_title_id, $arr_data['categories']);
            }
            
            $arr_data['title_id'] = $int_title_id;
        }
        
        public function insertPressTitleCategories($int_title_id, $arr_categories) {
            
            foreach($arr_categories as $int_category_id) {
                
                $arr_values = array(
                    'title_id' => $int_title_id,
                    'category_id' => $int_category_id
                );
                
                echo "  - sql executed: " . $this->insert('press_title_category', $arr_values);
            }
            
        }
        
        public function insertRadioStation($arr_data) {
            
            echo __METHOD__ . "\n";
            
            if(!isset($arr_data['station_name']) || !isset($arr_data['station_slug'])) {
                throw new Exception('Could not insert a new radio station record because not required data is present.');
            }
            
            $arr_values = array(
                'station_name' => $arr_data['station_name'],
                'station_slug' => $arr_data['station_slug']
            );
            
            echo "  - sql executed: " . $this->insert('radio_station', $arr_values);
            
            $int_station_id = 267;
            
            $arr_data['station_id'] = $int_station_id;
        }
        
    }
    
    class DataGateway_Cache extends DataGateway {
        
        private $bln_locked_cache = false;
        
        public function __construct() {
        }
        
        public function selectPressTitle($int_id) {
        
            echo __METHOD__ . "\n";
            
            $arr = array(
                145 => array(
                    'title_id' => 145,
                    'title_name' => 'Financial Times',
                    'title_slug' => 'financial-times',
                    'title_type' => 1
                )
            );
            
            if(isset($arr[$int_id])) {
                
                echo "  - found title [" . $int_id . "] in cache" . "\n";
            
                return array($arr[$int_id]);
            }
            
            return null;
        }
        
        public function cachePressTitle($arr_data) {
            
            echo __METHOD__ . "\n";
            
            $int_title_id = isset($arr_data['title_id']) ? $arr_data['title_id'] : null;
            
            if(isset($arr_data['categories']) && is_array($arr_data['categories'])) {
            
                $this->cachePressTitleCategories($int_title_id, $arr_data['categories']);
            }
        }
        
        public function cachePressTitleCategories($int_title_id, $arr_categories) {
            
            $arr_values = array('title_id' => $int_title_id);
            
            foreach($arr_categories as $int_category_id) {
                
                $str_key = $this->convertCategoryIdToKey($int_category_id);
                $str_column = 'is_' . $str_key;
                
                $arr_values[$str_column] = 1;
                
            }
            
            if(count($arr_values) > 1) {
                
                echo "  - sql executed: " . $this->insert('press_title_category_cache', $arr_values);
            }
            
        }
        
        public function convertCategoryIdToKey($int_id) {
            
            $arr = array(
                1 => 'news',
                2 => 'fashion',
                3 => 'mens',
                4 => 'womens'
            );
            
            return isset($arr[$int_id]) ? $arr[$int_id] : null;
            
        }
        
        public function clearCache() {
            echo __METHOD__ . "\n";
            echo '  - cached cleared.' . "\n";
        }
        
        public function toggleCacheLock() {
            echo __METHOD__ . "\n";
            
            if($this->bln_locked_cache) {
                echo '  - cached unlocked.' . "\n";
                $this->bln_locked_cache = false;
            } else {
                echo '  - cached locked.' . "\n";
                $this->bln_locked_cache = true;
            }
            
        }
        
    }
    
    class DataGateway_Legacy extends DataGateway {
        
        public function __construct() {
        }
        
        public function insertPressTitle($arr_data) {
            
            echo __METHOD__ . "\n";
            
            if(!isset($arr_data['title_name']) || !isset($arr_data['title_type'])) {
                throw new Exception('Could not insert a new press title record because required data is not present.');
            }
            
            $arr_values = array(
                'title_name' => $arr_data['title_name'],
                'title_type' => $arr_data['title_type']
            );
            
            echo "  - sql executed: " . $this->insert('press_title', $arr_values);
            
            $int_title_id = 145;
            
            $arr_data['title_id'] = $int_title_id;
        }
        
        public function insertRadioStation($arr_data) {
            
            echo __METHOD__ . "\n";
            
            if(!isset($arr_data['station_name'])) {
                throw new Exception('Could not insert a new radio station record because required data is not present.');
            }
            
            $arr_values = array(
                'station_name' => $arr_data['station_name']
            );
            
            echo "  - sql executed: " . $this->insert('radio_station', $arr_values);
            
            $int_station_id = 267;
            
            $arr_data['station_id'] = $int_station_id;
        }
        
    }
    
    class DataGateway_Preparer {
        
        public function __construct() {
        }
        
        static public function generateSlug($str) {
            return str_replace(' ', '-', strtolower($str));
        }
        
        public function prepareData($arr_data) {
            
            echo __METHOD__ . "\n";
            
            foreach($arr_data as $str_key => $mix_value) {
                $arr_matches = array();
                if(preg_match('/(.*)_name$/', $str_key, $arr_matches)) {
                    
                    $str_key_slug = $arr_matches[1] . '_slug';
                    $arr_data[$str_key_slug] = self::generateSlug($mix_value);
                    
                    echo '  - ' . $str_key_slug . ' [' . $arr_data[$str_key_slug] . '] generated from ' . $str_key . ' [' . $arr_data[$str_key] . '].' . "\n";
                    
                }
            }
            
        }
        
        
    }
    
    class Notifier {
        
        public function __construct() {
        }
        
        public function publishException($obj_exception) {
            echo __METHOD__ . "\n";
            echo '  - notified developers that an exception [' . get_class($obj_exception) . '] has occurred.' . "\n";
            echo '  - exception message: ' . $obj_exception->getMessage() . "\n";
        }
        
        
    }
    
?>
